﻿using System;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using S3.DataModel;
using S3.Biz;
using S3.Common;

namespace S3
{
    public partial class ManageTimeBlockForm : S3FormBase
    {
        private bool _isChanged;

        public ManageTimeBlockForm()
        {
            InitializeComponent();
            InitKindOfMachines();
            //LoadBoardOfPrice();
        }

        private void InitKindOfMachines()
        {
            //Load kind of machines            
            comboKindOfMachines.DataSource = Utils.KindOfMachinesList();

            comboKindOfMachines.SelectedIndex = 0;
        }

        private void LoadBoardOfPrice()
        {
            //Load current info
            using (var timeBlockUnitPriceManager = new TimeBlockUnitPriceManager())
            {
                string currentMachine = comboKindOfMachines.SelectedItem + "";
                lblKindOfMachine.Text = currentMachine;

                if (string.IsNullOrEmpty(currentMachine)) return;

                TimeBlockUnitPrice machine = timeBlockUnitPriceManager.GetOne(currentMachine);

                decimal decFirstBlockTime = 0;
                decimal decNextBlockTime = 0;
                decimal decFirstBlockMoney = 0;
                decimal decNextBlockMoney = 0;
                txtPricePerHour.Text = "0";
                lblTimePlayed.Text = "0";
                txtMoneyTest.Text = "0";
                trackTestMinutes.Value = 0;
                //decimal decTotalMoney = 0;

                if (machine != null)
                {
                    decFirstBlockTime = Convert.ToDecimal(machine.FirstBlockTime);
                    decNextBlockTime = Convert.ToDecimal(machine.NextBlockTime);
                    decFirstBlockMoney = machine.FirstBlockUnitPrice.HasValue ? machine.FirstBlockUnitPrice.Value : 0;
                    decNextBlockMoney = machine.NextBlockUnitPrice.HasValue ? machine.NextBlockUnitPrice.Value : 0;
                    txtPricePerHour.Text = machine.TotalMoney.HasValue
                                               ? Utils.StringFormatMoney(machine.TotalMoney.Value)
                                               : "0";

                    //decTotalMoney = (decFirstBlockMoney) + decNextBlockTime != 0 ? ((decNextBlockMoney / decNextBlockTime) * (60 - decFirstBlockTime)) : 0;
                }

                numFirstBlockMinutes.Value = decFirstBlockTime;
                numNextBlockMinutes.Value = decNextBlockTime;

                txtFirstBlockPrice.Text = Utils.StringFormatMoney(decFirstBlockMoney);
                txtNextBlockPrice.Text = Utils.StringFormatMoney(decNextBlockMoney);


                //txtPricePerHour.Text = Utils.StringFormatMoney(decTotalMoney);

                if (numFirstBlockMinutes.Value == 1 && numNextBlockMinutes.Value == 1)
                {
                    radioMethodChargePerOneMinutes.Checked = true;
                }
                else if (numFirstBlockMinutes.Value == 10 && numNextBlockMinutes.Value == 1)
                {
                    radioMethod10And1.Checked = true;
                }
                else if (numFirstBlockMinutes.Value == 10 && numNextBlockMinutes.Value == 5)
                {
                    radioMethod10And5.Checked = true;
                }
                else if (numFirstBlockMinutes.Value == 5 && numNextBlockMinutes.Value == 5)
                {
                    radioMethodEach5.Checked = true;
                }
                else if (numFirstBlockMinutes.Value == 10 && numNextBlockMinutes.Value == 10)
                {
                    radioMethodEach10.Checked = true;
                }
                else
                {
                    radioMethodChargeCustom.Checked = true;
                }
            }
        }

        private void comboKindOfMachines_SelectedIndexChanged(object sender, EventArgs e)
        {
            LoadBoardOfPrice();
        }

        private void CalculateBlock(int firstBlockTime, int nextBlockTime)
        {
            decimal pricePerHour = 0;
            if (txtPricePerHour.Text != string.Empty && decimal.TryParse(txtPricePerHour.Text, out pricePerHour) &&
                !radioMethodChargeCustom.Checked)
            {
                numFirstBlockMinutes.Value = Convert.ToDecimal(firstBlockTime);
                numNextBlockMinutes.Value = Convert.ToDecimal(nextBlockTime);

                decimal pricePerMinutes = pricePerHour/60;

                decimal decFirstBlockPrice = firstBlockTime*pricePerMinutes;
                decimal decNextBlockPrice = nextBlockTime*pricePerMinutes;

                txtFirstBlockPrice.Text = Utils.StringFormatMoney(decFirstBlockPrice);
                txtNextBlockPrice.Text = Utils.StringFormatMoney(decNextBlockPrice);

                //numFirstBlockMinutes.Enabled = false;
                //numNextBlockMinutes.Enabled = false;
                //txtFirstBlockPrice.ReadOnly = true;
                //txtNextBlockPrice.ReadOnly = true;                
            }
            //else
            //{
            //    decimal fp = 0;
            //    decimal sp = 0;
            //    if (decimal.TryParse(txtFirstBlockPrice.Text, out fp) && decimal.TryParse(txtNextBlockPrice.Text, out sp))
            //    {
            //        decimal fm = numFirstBlockMinutes.Value;
            //        decimal sm = numNextBlockMinutes.Value;
            //        txtPricePerHour.Text = Utils.StringFormatMoney((fp) + sm == 0 ? 0 : (sp * (60 - fm) / sm));
            //    }
            //    else
            //    {
            //        txtPricePerHour.Text = Utils.StringFormatMoney(0);
            //    }
            //}
        }


        private void radioMethodChargePerOneMinutes_CheckedChanged(object sender, EventArgs e)
        {
            CalculateBlock(1, 1);
        }

        private void radioMethod10And1_CheckedChanged(object sender, EventArgs e)
        {
            CalculateBlock(10, 1);
        }

        private void radioMethod10And5_CheckedChanged(object sender, EventArgs e)
        {
            CalculateBlock(10, 5);
        }

        private void radioMethodEach5_CheckedChanged(object sender, EventArgs e)
        {
            CalculateBlock(5, 5);
        }

        private void radioMethodEach10_CheckedChanged(object sender, EventArgs e)
        {
            CalculateBlock(10, 10);
        }

        private void radioMethodChargeCustom_CheckedChanged(object sender, EventArgs e)
        {
            //numFirstBlockMinutes.Enabled = true;
            //numNextBlockMinutes.Enabled = true;
            //txtFirstBlockPrice.ReadOnly = false;
            //txtNextBlockPrice.ReadOnly = false;
        }

        /// <summary>
        /// Close form if click ESC
        /// </summary>
        /// <param name="msg"></param>
        /// <param name="keyData"></param>
        /// <returns></returns>

        private void btnCancel_Click(object sender, EventArgs e)
        {
            Close();
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            if (comboKindOfMachines.Text != string.Empty && txtPricePerHour.Text != string.Empty)
            {
                using (var timeBlockUnitPriceManager = new TimeBlockUnitPriceManager())
                {
                    var entry = new TimeBlockUnitPrice
                                    {
                                        TimingServiceType = comboKindOfMachines.Text,
                                        FirstBlockTime = Convert.ToInt32(numFirstBlockMinutes.Value),
                                        FirstBlockUnitPrice = Convert.ToDecimal(txtFirstBlockPrice.EditValue),
                                        NextBlockTime = Convert.ToInt32(numNextBlockMinutes.Value),
                                        NextBlockUnitPrice = Convert.ToDecimal(txtNextBlockPrice.EditValue),
                                        TotalMoney = Convert.ToDecimal(txtPricePerHour.Text),
                                    };

                    timeBlockUnitPriceManager.Save(entry);
                }

                XtraMessageBox.Show("Đã cập nhật xong dữ liệu của " + comboKindOfMachines.Text);
                _isChanged = true;
            }
        }

        private void btnClose_Click(object sender, EventArgs e)
        {
            Close();
        }

        private void btnCalculate_Click(object sender, EventArgs e)
        {
            decimal decMoneyPerHour = 0;
            if (decimal.TryParse(txtPricePerHour.Text.Trim(), out decMoneyPerHour))
            {
                txtPricePerHour.Text = Utils.StringFormatMoney(decMoneyPerHour);
            }
            else
            {
                txtPricePerHour.Text = "0";
            }

            CalculateBlock(Convert.ToInt32(numFirstBlockMinutes.Value), Convert.ToInt32(numNextBlockMinutes.Value));
        }

        private void ManageTimeBlockForm_FormClosing(object sender, FormClosingEventArgs e)
        {
            //if (_isChanged)
            //    _ownerForm.ShowData();
        }

        private void txtFirstBlockPrice_Leave(object sender, EventArgs e)
        {
            decimal pricePerHour = Convert.ToDecimal(txtPricePerHour.Text);
            decimal firstBlockPrice = Convert.ToDecimal(txtFirstBlockPrice.EditValue);
            decimal remainMoney = pricePerHour - firstBlockPrice;

            decimal minutesFirstBlock = numFirstBlockMinutes.Value;
            decimal minutesNextBlock = numNextBlockMinutes.Value;

            decimal minutesRemain = 60 - minutesFirstBlock;
            decimal blockRemain = minutesNextBlock == 0? 0 : minutesRemain/minutesNextBlock;

            txtNextBlockPrice.Text = blockRemain == 0? "0" : Utils.StringFormatMoney(remainMoney/blockRemain);
        }

        #region ProcessCmdKey

        protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
        {
            try
            {
                if (msg.WParam.ToInt32() == (int) Keys.Escape)
                {
                    Close();
                }
                else
                {
                    return base.ProcessCmdKey(ref msg, keyData);
                }
            }
            catch (Exception Ex)
            {
                XtraMessageBox.Show("Key Overrided Events Error:" + Ex.Message);
            }

            return base.ProcessCmdKey(ref msg, keyData);
        }

        #endregion

        private void trackTestMinutes_EditValueChanged(object sender, EventArgs e)
        {
            decimal playedMinutes = trackTestMinutes.Value * 5;
            lblTimePlayed.Text = playedMinutes.ToString();
            string kindOfMachine = comboKindOfMachines.Text;

            int firstBlockMinutes = Convert.ToInt32(numFirstBlockMinutes.Value);
            decimal firstBlockUnitPrice = Convert.ToDecimal(txtFirstBlockPrice.EditValue);
            int nextBlockMinutes = Convert.ToInt32(numNextBlockMinutes.Value);
            decimal nextBlockUnitPrice = Convert.ToDecimal(txtNextBlockPrice.EditValue);

            using (DailyTimingEntryManager timeBlockUnitPriceManager = new Biz.DailyTimingEntryManager())
            {
                decimal totalMoney = timeBlockUnitPriceManager.CalcGameMoneyByBlock(playedMinutes, kindOfMachine, firstBlockMinutes, firstBlockUnitPrice, nextBlockMinutes, nextBlockUnitPrice);
                txtMoneyTest.Text = Utils.StringFormatMoney(totalMoney);
            }
        }

    }
}